using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using eShop.Business;
using eShop.Model;
using Entity;
using System.Web;

public partial class invoice_print_page : SitePublicBasePage
{
    private Order _order;
    public Order Order
    {
        get
        {
            if (_order == null)
            {
                Int32 orderNumber=0;
                //if(SessionManager.OrderNumber != null)
                //    orderNumber = SessionManager.OrderNumber;
                //else 
                if (HttpContext.Current.Request.QueryString["oid"] != null && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["oid"]))
                    orderNumber = Convert.ToInt32(HttpContext.Current.Request.QueryString["oid"]);

                    _order = OrderManager.GetOrderById(orderNumber);
            }
            return _order;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            if (Order == null)
            {
                Response.Redirect(eShop.Business.CustomSiteMap.OrderSummary);
            }

            if (Order.OrderItems == null || Order.OrderItems.Count <= 0)
            {
                Response.Redirect(eShop.Business.CustomSiteMap.ShoppingCart);
            }
            BindData();
        }
    }

    private void BindData()
    {
        litCompanyaddress.Text = Server.HtmlDecode(string.Format("{0}<br/>{1}, {2} {3}<br/>Phone: {4}<br/>", SiteSettings.CompanyAddress, SiteSettings.CompanyCity, SiteSettings.CompanyState, SiteSettings.CompanyZip, SiteSettings.CompanyPhone));

        BindProductList();

        
        Address shippingAddress = Order.ShippingAddress;
        

        lblShipmentStatus.Text = Order.ShippingStatus.ToString();
        lblDate.Text = Order.OrderDate.ToShortDateString();
        lblOrderNo.Text = Order.OrderNumber + Order.OrderId.ToString();
        lblSubTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.OrderItems.TotalPrice);
        lblShippingHandling.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.ShippingCost);
        lblTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TotalAmount);
        lblCustomerNumber.Text = Order.UserId.ToString().PadLeft(10, '0');
        lblPaymentMethod.Text = Order.PaymentType.ToString();
        lblPaymentStatus.Text = Order.OrderStatus.ToString();

        lblShippingName.Text = shippingAddress.Name;
        lblShippingAddress.Text = shippingAddress.AddressLine1 + "\n" + shippingAddress.AddressLine2;
        lblShippingCountry.Text = shippingAddress.Country;
        lblShippingPhone.Text = shippingAddress.PhoneNumber;
        lblShippingCityStateZip.Text = string.Format("{0}, {1} {2}", shippingAddress.City, shippingAddress.State, shippingAddress.Zip);

        
        lblTransactionId.Text = "<b>Transaction id</b> : " + Order.TransectionId;
        lblStatusMsg.Text = "<b>Order status</b> : " + Order.PaymentStatus;

        if (!String.IsNullOrEmpty(Order.SpecialInstruction))
        {
            lblComments.Visible = true;
            lblComments.Text = "<b>Comments</b> : " + Order.SpecialInstruction;
        }
    }

    private void BindProductList()
    {
        rptProduct.DataSource = Order.OrderItems;
        rptProduct.DataBind();
        if (rptProduct.Items.Count == 0) 
        {
            rptProduct.Visible = false;
        }
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            OrderItem cartItem = (OrderItem)e.Item.DataItem;
            if (cartItem !=null)
            {
                Label lblProductName = (Label)e.Item.FindControl("lblProductName");
                lblProductName.Text = cartItem.ProductName;

                Label lblItemNumber = (Label)e.Item.FindControl("lblItemNumber");
                lblItemNumber.Text = cartItem.ProductCode;
                
                Label lblItemPrice = (Label)e.Item.FindControl("lblItemPrice");
                lblItemPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.UnitPrice);

                Label lblQuantity = (Label)e.Item.FindControl("lblQuantity");
                lblQuantity.Text = cartItem.Quantity.ToString();

                Label lblItemTotal = (Label)e.Item.FindControl("lblItemTotal");
                lblItemTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.TotalPrice);
            }

        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
        }
    }
    protected void imbPrintInvoice_Click(object sender, EventArgs e)
    {
        imbPrintInvoice.Visible = false;
        PrintInvoice();
        Session["PrintOrderList"] = null;
    }

    protected void PrintInvoice()
    {
        string script = "<script type=\"text/javascript\" language=\"javascript\">window.print();</script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(),"printscript",script);
        
    }
   
}
